package com.zhaolang.qogir.ui.manage;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.yyydjk.library.DropDownMenu;
import com.zhaolang.qogir.R;
import com.zhaolang.qogir.bean.ManageAllComments;
import com.zhaolang.qogir.ui.BaseActivity;
import com.zhaolang.qogir.ui.ContextState;
import com.zhaolang.qogir.ui.TitleView;
import com.zhaolang.qogir.ui.adapter.manage.ListDropDownAdapter;
import com.zhaolang.qogir.ui.adapter.manage.ManageAllCommentAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.ButterKnife;

public class ActivityManageAllComment extends BaseActivity implements View.OnClickListener {
    private String TAG = "ActivityManageAllComment";
    DropDownMenu mDropDownMenu;
    private List<View> popupViews = new ArrayList<>();

    private TitleView mTitle;
    private String headers[] = {"回复情况", "类型", "内容"};

    private String headerFeedback[] = {"全部", "已回复", "未回复"};
    private String headerStar[] = {"全部", "好评（4-5星）", "中评（3星）", "差评（1-2星）"};
    private String headerContents[] = {"全部", "有内容", "有图片"};

    private ListDropDownAdapter contentsAdapter;
    private ListDropDownAdapter feedbackAdapter;
    private ListDropDownAdapter starAdapter;
    private ListView middleListView;
    private ManageAllCommentAdapter allCommentAdapter;
    /**
     * 下一批数据开始的位置
     */
    private int mStartIndex = 0;

    /**
     * 下一批数据的数量
     */
    private int mMaxCount = 20;

    /**
     * 数据总数
     */
    private int mTotalCount = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage_comment);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        View title = findViewById(R.id.vg_title);
        mTitle = new TitleView(this, title);
        mTitle.setCenterText(R.string.manage_product_comment, null);
        mTitle = new TitleView(getApplicationContext(), title);
        mTitle.setLeftIcon(R.drawable.btn_back, this);

        mDropDownMenu = findViewById(R.id.dropDownMenu);

        //init feedback menu
        final ListView feedbackView = new ListView(this);
        feedbackView.setDividerHeight(0);
        feedbackAdapter = new ListDropDownAdapter(this, Arrays.asList(headerFeedback));
        feedbackView.setAdapter(feedbackAdapter);

        //init star menu
        final ListView starView = new ListView(this);
        starView.setDividerHeight(0);
        starAdapter = new ListDropDownAdapter(this, Arrays.asList(headerStar));
        starView.setAdapter(starAdapter);

        //init contents menu
        final ListView contentsView = new ListView(this); //gird:准备
        contentsAdapter = new ListDropDownAdapter(this, Arrays.asList(headerContents));
        contentsView.setDividerHeight(0);
        contentsView.setAdapter(contentsAdapter);


        feedbackView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                feedbackAdapter.setCheckItem(position);
                mDropDownMenu.setTabText(position == 0 ? headers[2] : headerFeedback[position]);
                mDropDownMenu.closeMenu();
            }
        });

        starView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                feedbackAdapter.setCheckItem(position);
                mDropDownMenu.setTabText(position == 0 ? headers[1] : headerStar[position]);
                mDropDownMenu.closeMenu();
            }
        });

        contentsView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                feedbackAdapter.setCheckItem(position);
                mDropDownMenu.setTabText(position == 0 ? headers[0] : headerContents[position]);
                mDropDownMenu.closeMenu();
            }
        });

        //init content view
        final View contentView = getLayoutInflater().inflate(R.layout.manage_comment_middle_view, null);
        contentView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        popupViews.add(contentsView);
        popupViews.add(starView);
        popupViews.add(feedbackView);

        mDropDownMenu.setDropDownMenu(Arrays.asList(headers), popupViews, contentView);

        middleListView = contentView.findViewById(R.id.manage_middle_lv);
        allCommentAdapter = new ManageAllCommentAdapter(this);
        middleListView.setAdapter(allCommentAdapter);
        allCommentAdapter.setData(getAllCommentInfo());

        // 设置滑动监听
        middleListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            // 滑动状态发生改变时回调
            // SCROLL_STATE_IDLE 闲置状态，此时没有滑动
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState) {
                    case SCROLL_STATE_IDLE:
                        int position = middleListView.getLastVisiblePosition();
                        // 如果屏幕上可见的最后一项是当前适配器数据源的最后一项，
                        // 并且数据还没有加载完，就加载下一批数据。
                        if (position == getAllCommentInfo().size() - 1 && position != mTotalCount - 1) {
                            mStartIndex += mMaxCount;
                            // 加载下一批数据
                            new LoadDataTask().execute();
                        } else if (position == getAllCommentInfo().size() - 1 && position == mTotalCount -
                                1) {
                            Toast.makeText(getApplicationContext(), "没有更多数据了", Toast.LENGTH_SHORT).show();
                        }
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                                 int totalItemCount) {
            }
        });
    }

    /**
     * Just for debugging comment info module
     *
     * @return
     */
    private List<ManageAllComments> getAllCommentInfo() {
        List<ManageAllComments> list = new ArrayList<>();
        ManageAllComments allComments = new ManageAllComments();
        allComments.setManageOrderTime("2018/8/9 11:30");
        allComments.setManageTvCommentContents(this.getResources().getText(R.string.ui_manage_comment_msg).toString());
        allComments.setManageTvDelivery("1.0");
        allComments.setManageTvPackage("1.0");
        allComments.setManageTvTaste("1.0");
        allComments.setManageTvUserName("某某用户");
        list.add(0, allComments);
        list.add(1, allComments);
        list.add(2, allComments);
        return list;
    }

    @Override
    public void onBackPressed() {
        if (mDropDownMenu.isShowing()) {
            mDropDownMenu.closeMenu();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case TitleView.ID_LEFT:
                finish();
                break;
            default:
                break;
        }
    }

    class LoadDataTask extends AsyncTask<Void, Void, List<String>> {

        @Override
        protected void onPreExecute() {
            // 显示进度条
            showWaitingDialog((ContextState) null);
        }

        @Override
        protected List<String> doInBackground(Void... params) {
            // 模拟耗时
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(List<String> strings) {
            // 隐藏进度条
            dimessWaitingDialog();
        }
    }
}
